package controller.action;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.domain.WeatherBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;



public class WeatherAction implements Action {
	public String process(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		/*System.out.println("command=" + request.getHeader("command"));
		Document doc = Jsoup.connect("http://m.naver.com").get();
		//System.out.println(doc);
		Elements reff = doc.select("li.toif2_l");
		Elements url = doc.getElementsByClass("toif2_aw");
		Elements location = doc.getElementsByClass("toif2_h");
		Elements degree = doc.getElementsByClass("toif2_deg");
		Elements mi = doc.getElementsByClass("mi");
		Elements state = doc.getElementsByClass("imwt");
		FileWriter out = new FileWriter(
				"C:\\Users\\오션45\\Desktop\\Naver_UX\\WebContent\\weather.json");
		for (int i = 5; i < reff.size(); i++) {
			out.write(url.get(i).attr("href") + "  " + location.get(i).text()
					+ "  " + degree.get(i - 5).text() + "  "
					+ mi.get(i - 5).text() + "  " + state.get(i - 5).text()
					+ "\n");
		}
			out.close();*/
		ArrayList<WeatherBean> alldata = null;
		HashMap<String, Object> map = new HashMap<String, Object>();
			try {
				Document doc = Jsoup.connect("http://m.naver.com").get();
				Elements reff = doc.select("li.toif2_l");
				Elements url = doc.getElementsByClass("toif2_aw");
				Elements location = doc.getElementsByClass("toif2_h");
				Elements degree = doc.getElementsByClass("toif2_deg");
				Elements mi = doc.getElementsByClass("mi");
				Elements state = doc.getElementsByClass("imwt");
				alldata = new ArrayList<WeatherBean>();
				for (int i = 5; i < reff.size(); i++) {
					alldata.add(i - 5, new WeatherBean(url.get(i).attr("href"), location
							.get(i).text(), degree.get(i - 5).text(), mi.get(i - 5)
							.text(), state.get(i - 5).text()));
				}	
				JSONArray jsonarray = JSONArray.fromObject(alldata);
				map.put("dailyList", jsonarray);
				JSONObject jsonObject = JSONObject.fromObject(map);
				System.out.println("json-" + jsonObject);
				PrintWriter out=response.getWriter();
				out.print(jsonObject.toString());

			} catch (IOException e) {
				e.printStackTrace();
			}
		return null;
	}

}

	
	/*ArrayList<WeatherBean> alldata = null;
	HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			Document doc = Jsoup.connect("http://m.naver.com").get();
			Elements reff = doc.select("li.toif2_l");
			Elements url = doc.getElementsByClass("toif2_aw");
			Elements location = doc.getElementsByClass("toif2_h");
			Elements degree = doc.getElementsByClass("toif2_deg");
			Elements mi = doc.getElementsByClass("mi");
			Elements state = doc.getElementsByClass("imwt");
			alldata = new ArrayList<WeatherBean>();
			for (int i = 5; i < reff.size(); i++) {
				alldata.add(i - 5, new WeatherBean(url.get(i).attr("href"), location
						.get(i).text(), degree.get(i - 5).text(), mi.get(i - 5)
						.text(), state.get(i - 5).text()));
			}	
			JSONArray jsonarray = JSONArray.fromObject(alldata);
			map.put("dailyList", jsonarray);
			JSONObject jsonObject = JSONObject.fromObject(map);
			System.out.println("json-" + jsonObject);
			PrintWriter out=response.getWriter();
			out.print(jsonObject.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}*/
	